//
//  LYTabBar.swift
//  Swift---新浪微博练习代码
//
//  Created by Mr.Yan on 2017/6/29.
//  Copyright © 2017年 Mr.Yan. All rights reserved.
//

import UIKit

class LYTabBar: UITabBar {
    //01声明闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func composeButtonClick(){
    
        //03执行闭包
        closure?()
    
    }
    //设置视图
    private func  setupUI(){
    addSubview(composeButton)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    //重写设置uiTabbarButton宽度
        let w = frame.width * 0.2
        //记录遍历到第几个下标
        var index: CGFloat = 0
        //遍历子控件 uibarbutton
        for view in subviews{
            if view.isKind(of: NSClassFromString("UITabBarButton")!){
            //宽
                view.frame.size.width = w
                //x轴
                view.frame.origin.x = w * index
                //index递增
                index += 1
                //如果等于2需要再加1 给编辑按钮留位置
                if index == 2 {
                    index += 1
                }
            
            }
        
        }
        composeButton.center.x = frame.size.width * 0.5
        composeButton.center.y = frame.size.height * 0.5
    }
    
    //懒加载控件
    //编写按钮
    private lazy var composeButton:UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(composeButtonClick), for: UIControlEvents.touchUpInside)
        //设置image和背景图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        btn.sizeToFit()
        return btn
    }()
    
}
